package com.molichuxing.services.infrastructure.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.molichuxing.framework.utils.CollectionUtils;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.InsuranceCommercialMapper;
import com.molichuxing.services.infrastructure.dto.request.create.InsuranceCommercialCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.InsuranceCommercialModifyDto;
import com.molichuxing.services.infrastructure.dto.response.InsuranceCommercialDto;
import com.molichuxing.services.infrastructure.service.InsuranceCommercialService;

/**
 * <p>
 * 商业险详细 服务实现类
 * </p>
 *
 * @author 
 * @since 2019-08-15
 */
@Service("insuranceCommercialService")
public class InsuranceCommercialServiceImpl implements InsuranceCommercialService {

  private static final Logger log = Logger.getLogger(InsuranceCommercialServiceImpl.class);

  @Resource
  private InsuranceCommercialMapper insuranceCommercialMapper;

  @Override
  public Map<Integer, InsuranceCommercialDto> getInsuranceCommercialAmount(List<Integer> insuranceIds) {
    List<InsuranceCommercialDto> list = Convert.toInsuranceCommercialDto(insuranceCommercialMapper.getInsuranceCommercialAmount(insuranceIds));
    if(null == list|| list.size() == 0) {
      return null;
    }
    Map<Integer, InsuranceCommercialDto> result = new HashMap<>();
    list.forEach(e->{
      if(null != e) {
        result.put(e.getInsureId(),e);
      }
    });
    return result;
  }

  @Override
  public List<InsuranceCommercialDto> getInsuranceCommercialList(Integer insuranceId) {
    return Convert.toInsuranceCommercialDto(insuranceCommercialMapper.getInsuranceCommercialList(insuranceId));
  }

  @Override
  public Boolean modifyCommercial(int insuranceId, List<InsuranceCommercialModifyDto> modifyDtoList) {
    for(InsuranceCommercialModifyDto modifyDto:modifyDtoList) {
      modifyDto.setInsureId(insuranceId);
      insuranceCommercialMapper.modifyCommercial(Convert.toInsuranceCommercialBean(modifyDto));
    }
    return true;
  }

  @Override
  public Boolean createCommercial(int insuranceId, List<InsuranceCommercialCreateDto> createDtoList) {
    for(InsuranceCommercialCreateDto createDto:createDtoList) {
      createDto.setInsureId(insuranceId);
      insuranceCommercialMapper.createCommercial(Convert.toInsuranceCommercialBean(createDto));
    }
    return true;
  }

  @Override
  public int createBatchCommercial(List<InsuranceCommercialCreateDto> dataList) {
    if (CollectionUtils.isNotEmpty(dataList)) {
      return insuranceCommercialMapper.createBatchCommercial(Convert.toInsuranceCommercialBeanList(dataList));
    }
    return 0;
  }

	/**
	 * 根据商业险id获取保险保费信息
	 * 
	 * @param insuranceIds
	 * @return
	 */
	@Override
	public Map<Integer, List<InsuranceCommercialDto>> getInsuranceCommercialList(List<Integer> insuranceIds) {
		Map<Integer, List<InsuranceCommercialDto>> result = null;
		List<InsuranceCommercialDto> list = Convert
				.toInsuranceCommercialDto(insuranceCommercialMapper.getInsuranceCommercials(insuranceIds));
		if (null == list || list.size() == 0) {
			return result;
		}
		result = new HashMap<>();
		List<InsuranceCommercialDto> dtos = new ArrayList<>();
		Integer insuranceId = list.get(0).getInsureId();
		for (InsuranceCommercialDto insuranceCommercialDto : list) {
			if (!insuranceId.equals(insuranceCommercialDto.getInsureId())) {
				result.put(insuranceId, dtos);
				insuranceId = insuranceCommercialDto.getInsureId();
				dtos = new ArrayList<>();
			}
			dtos.add(insuranceCommercialDto);
		}

		return result;
	}
}
